<?php
/**
 * 汇付支付渠道类
 * User: 13651
 * Time: 2025/10/23 09:31
 */

namespace App\Services;

use App\Api\Controllers\HuiPay\HuifuBaseController;
use App\Exceptions\BizException;
use App\Extensions\BaseService;
use App\Models\HuifuBank;
use App\Models\HuifuConfigs;
use App\Models\HuifuMccCode;
use App\Models\MyBankCategory;
use App\Models\Store;
use App\Models\StoreBank;
use App\Models\StoreImg;
use App\Models\StorePayWay;
use App\Models\User;
use App\Models\VbillStoreCategory;
use App\Param\LeshuaUnion\GetChannelDetailsParams;
use App\Repository\HuifuStoreRepo;
use App\Repository\StorePayRepo;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use function GuzzleHttp\Psr7\str;

class HuifuService  extends BaseService
{

    /**
     * 商户进件
     * @param $Store
     * @param $StoreBank
     * @param $StoreImg
     * @param $rate
     * @param $head_office_flag 1总部商户 0普通商户也就是下级商户
     * @param string $superiorStoreId
     * @return array|string[]
     */
    public function basicdataEntinfoService($Store,$StoreBank,$StoreImg,$rate,$head_office_flag,$superiorStoreId='')
    {

        if ($head_office_flag == 0)
        {
            if (empty($superiorStoreId))
                return $this->serviceError('上级商户不能为空');

            /**
             * 检测上级商户是否正确
             */
            $superiorStoreInfo = HuifuStoreRepo::getInstance()->getByStoreId($superiorStoreId);
            if ($superiorStoreInfo->pid != 0)
                return $this->serviceError('上级商户身份不为总部');
        }

        /**
         * 结算信息验证
         * 01 对私 1：对私法人账户 2：对私非法人账户
         * 02 对公
         */
        if ($StoreBank->store_bank_type == '01'){

            /**
             * 结算信息中 开户名!=营业执照人名称为非法人结算
             */
            if ( $Store->head_name != $StoreBank->store_bank_name)
            {
                $card_type = '2';
            }else{
                $card_type = '1';
            }
        }else{
            $card_type = '0';
        }

        /**
         * 联系人手机号
         */
        if (empty($Store->people_phone))
            return $this->serviceError('联系人手机号不能为空');

        /**
         * 联系人邮箱
         */
        if (empty($Store->store_email) || !strpos($Store->store_email,'@'))
            return $this->serviceError('联系人邮箱不能为空');

        /**
         * 验证图片
         */
        if (empty($StoreImg->store_license_img))
            return $this->serviceError('请上传营业执照照片');

        if (empty($StoreImg->head_sfz_img_b))
            return $this->serviceError('请上传法人身份证国徽面照片');

        if (empty($StoreImg->head_sfz_img_a))
            return $this->serviceError('请上传法人身份证人像面照片');

        // 企业类型 且结算信息为对公账户时需要开户许可证图片
        if ($Store->store_type == 2 && $StoreBank->store_bank_type=='01' && empty(1))
            return $this->serviceError('请上传开户许可证照片');

        /**
         * 结算信息-对私转账
         */
        if ($StoreBank->store_bank_type=='01')
        {
            if (empty($StoreImg->bank_img_a))
                return $this->serviceError('请上传银行卡正面照片');

            /**
             * 非法人结算
             */
            if ( $Store->head_name != $StoreBank->store_bank_name)
            {
                if (empty($StoreImg->bank_sfz_img_b))
                    return $this->serviceError('请上传非法人结算身份证国徽面照片');
                if (empty($StoreImg->bank_sfz_img_a))
                    return $this->serviceError('请上传非法人结算身份证人像面照片');
            }
        }

        if (empty($StoreImg->store_logo_img))
            return $this->serviceError('请上传门头照片');

        if (empty($StoreImg->store_img_c))
            return $this->serviceError('请上传经营店内照片');

        if (empty($StoreImg->store_img_a))
            return $this->serviceError('请上传店铺收银台/公司前台照片');

        /**
         * 获取渠道配置信息
         */
        $configInfo = HuifuConfigs::query()->where('config_id',1234)->first();
        if (empty($configInfo))
            return $this->serviceError('汇付渠道配置不存在');

        $configInfo = $configInfo->toArray();

        /**
         * 根据分类ID获取分类名称
         */
        $categoryInfo = VbillStoreCategory::query()->where('id',$Store->category_id)->first();
        Log::info('汇付渠道商户开户-根据分类ID获取信息'.json_encode($categoryInfo,JSON_UNESCAPED_UNICODE));
        if (empty($categoryInfo))
            return $this->serviceError('门店分类不存在');

        $mccCode = HuifuMccCode::query()->where('mcc_name',$categoryInfo->category_name)->first();
        if (empty($mccCode))
            return $this->serviceError('汇付行业编码未找到');

        /**
         * 入驻类型 该func仅限于个体、企业
         * key值为平台定义 1个体 2企业 3小微商户 4学校-教育行业
         * value为汇付侧的商户类型，
         * 1：政府机构
         * 2：国营企业
         * 3：私营企业
         * 4：外资企业
         * 5：个体工商户
         * 6：其它组织
         * 7：事业单位
         * 9：业主委员会
         */
        $merchantType = [
            1=>'5',
            2=>'3',
        ];

        // 营业执照有效期类型
        $license_validity_type =  $Store->store_license_time=='2999-12-31' ? "1":'0';

        /**
         * 获取省市区编码
         * PS：三方支付的省市区编码均一样，可区随行付、乐刷的数据库都可以
         */
        $provinceInfo = LeshuaUnionAreaCodeService::getInstance()->getByWhereService(['area_name'=>[$Store->province_name,$Store->city_name,$Store->area_name]]);
        if($provinceInfo->isEmpty())
            return $this->serviceError('城市编码缺省配置');
        $provincedata = $provinceInfo->toArray();

        try {
            $queryParam = [
                // 请求流水号
                'req_seq_id'=>date('YmdHis').mt_rand(),
                // 请求日期
                'req_date'=>date('Ymd'),
                // 渠道商户号
                'upper_huifu_id'=>$configInfo['sys_id'],
                // 商户名称
                'reg_name'=>$Store->store_name,
                // 商户简称
                'short_name'=>$Store->store_short_name,
                // 小票名称
                'receipt_name'=>$Store->store_short_name,
                // 公司类型
                'ent_type'=>$merchantType[$Store->store_type],
                // 行业编码
                'mcc'=>$mccCode->mcc_code,
                // 经营类型 1：实体，2：虚拟 ；
                'busi_type'=>'1',
                // 场景类型ONLINE：线上场景；OFFLINE：线下场景；ALL：线上线下；
                'scene_type'=>'ALL',
                'license_code'=>$Store->store_license_no,
                // 营业执照图片
                'license_pic'=>$this->uploadImg($StoreImg->store_license_img,'F07',$configInfo),
                // 营业执照类型
                'license_type'=>'NATIONAL_LEGAL',
                // 营业执照有效期 0：非长期有效,1：长期有效；
                'license_validity_type'=> $license_validity_type,
                // 营业执照开始时间
                'license_begin_date'=>date('Ymd',strtotime($Store->store_license_stime)),
                // 营业执照结束时间
                'license_end_date'=>$license_validity_type == '1' ? '':date('Ymd',strtotime($Store->store_license_end_time)),
                // 成立时间
                'found_date'=>date('Ymd',strtotime($Store->store_license_stime)),
                // 营业执照注册省
                'reg_prov_id'=>$provincedata[0]['area_code'],
                // 营业执照注册市
                'reg_city_id'=>$provincedata[1]['area_code'],
                // 营业执照注册区
                'reg_district_id'=>$provincedata[2]['area_code'],
                // 营业执照注册地址
                'reg_detail'=>$Store->store_address,
                // 经营区ID
                'district_id'=>$provincedata[2]['area_code'],
                // 经营详细地址
                'detail_addr'=>$Store->store_address,
                // 法人姓名
                'legal_name'=>$Store->head_name,
                // 法人证件类型 详情参考 https://paas.huifu.com/open/doc/api/#/api_ggcsbm?id=%e8%87%aa%e7%84%b6%e4%ba%ba%e8%af%81%e4%bb%b6%e7%b1%bb%e5%9e%8b
                'legal_cert_type'=>'00',
                // 法人身份证号码
                'legal_cert_no'=>$Store->head_sfz_no,
                // 有效期类型
                'legal_cert_validity_type'=>$Store->head_sfz_time == '2999-12-31' ? "1":'0',
                // 有效期开始
                'legal_cert_begin_date'=>date('Ymd',strtotime($Store->head_sfz_stime)),
                // 有效期结束
                'legal_cert_end_date'=>date('Ymd',strtotime($Store->head_sfz_time)),
                // 法人证件地址
                'legal_addr'=>$Store->store_address,
                // 法人身份证国徽面
                'legal_cert_back_pic'=>$this->uploadImg($StoreImg->head_sfz_img_b,'F03',$configInfo),
                // 法人身份证人像面
                'legal_cert_front_pic'=>$this->uploadImg($StoreImg->head_sfz_img_a,'F02',$configInfo),
                // 联系人姓名
                'contact_name'=>$Store->people,
                // 联系人手机号
                'contact_mobile_no'=>$Store->people_phone,
                // 联系人邮箱
                'contact_email'=>$Store->store_email,
                // 管理员账号
                'login_name'=>$Store->people_phone.date('YmdHis'),
                // 基本存款账户编号 当use_head_info_flag=Y时不填;
                'open_licence_no'=>'',
                // 银行卡配置
                'card_info'=>[
                    // 0：对公账户 1：对私法人账户 2：对私非法人账户 银行卡开户姓名 != 法人 为 对私非法人账户
                    'card_type'=>$card_type,
                    // 银行卡账户名
                    'card_name'=>$StoreBank->store_bank_name,
                    // 银行卡号
                    'card_no'=>$StoreBank->store_bank_no,
                    // 银行卡所在城市
                    'area_id'=>$StoreBank->bank_city_code,
                ],
                // 商户身份
                'head_office_flag'=>(string)$head_office_flag,
                // 是否使用上级资料
                'use_head_info_flag'=>'N',
                // 店铺门头照片
                'store_header_pic'=>$this->uploadImg($StoreImg->store_logo_img,'F22',$configInfo),
                // 店铺内景/工作区域照
                'store_indoor_pic'=>$this->uploadImg($StoreImg->store_img_c,'F24',$configInfo),
                // 店铺收银台/公司前台照
                'store_cashier_desk_pic'=>$this->uploadImg($StoreImg->store_img_a,'F105',$configInfo),
            ];

            /**
             * 结算信息为对公账户时
             */
            if ($card_type == '0')
            {
                $bankInfo = HuifuBank::query()->where('name',$StoreBank->sub_bank_name)->get();
                if ($bankInfo->isEmpty())
                    return $this->serviceError('未找到汇付支行编码');

                $huifuBankInfo = $bankInfo->toArray();
                // 对公账户需要获取支行的编码
                $queryParam['card_info']['branch_code'] = $huifuBankInfo[0]['code'];
                // 持卡人证件有效期截止日期 当cert_validity_type=0时必填；当cert_validity_type=1时为空
                $queryParam['card_info']['cert_end_date'] = '00';

                /**
                 * 对公非同名结算时必填项
                 */
                if ( $Store->head_name != $StoreBank->store_bank_name)
                {
                    /**
                     * 授权委托书
                     */
                    if (empty($StoreImg->store_auth_bank_img))
                        return $this->serviceError('请上传结算授权照片');

                    $queryParam['auth_entrust_pic'] = $this->uploadImg($StoreImg->store_auth_bank_img,'F15',$configInfo);
                }


            }else{
                // 持卡人的证件类型
                $queryParam['card_info']['cert_type'] = '00';
                if ($card_type == '1')// 1：对私法人账户
                {
                    // 取法人的身份证号码
                    $queryParam['card_info']['cert_no'] = $Store->head_sfz_no;
                    $queryParam['card_info']['cert_validity_type'] = $Store->head_sfz_time == '2999-12-31' ? "1":'0';
                    $queryParam['card_info']['cert_begin_date'] = date('Ymd',strtotime($Store->head_sfz_stime));
                    $queryParam['card_info']['cert_end_date'] = date('Ymd',strtotime($Store->head_sfz_time));
                    $queryParam['settle_cert_back_pic'] = $this->uploadImg($StoreImg->head_sfz_img_b,'F56',$configInfo);
                    $queryParam['settle_cert_front_pic'] = $this->uploadImg($StoreImg->head_sfz_img_a,'F55',$configInfo);
                }

                // 对私非法人账户
                if ($card_type == '2') // 2：对私非法人账户
                {
                    // 取法人的身份证号码
                    $queryParam['card_info']['cert_no'] = $StoreBank->bank_sfz_no;

                    //授权委托书
                    if (empty($StoreImg->store_auth_bank_img))
                        return $this->serviceError('请上传结算授权照片');

                    $queryParam['auth_entrust_pic'] = $this->uploadImg($StoreImg->store_auth_bank_img,'F15',$configInfo);
                    $queryParam['settle_cert_front_pic'] = $this->uploadImg($StoreImg->bank_sfz_img_a,'F55',$configInfo);
                    $queryParam['settle_cert_back_pic'] = $this->uploadImg($StoreImg->bank_sfz_img_b,'F56',$configInfo);
                }
                // 对私银行卡正面
                $queryParam['settle_card_front_pic'] = $this->uploadImg($StoreImg->bank_img_a,'F13',$configInfo);
            }

            /**
             * 企业商户
             */
            if ($Store->store_type == 2)
            {
                if (empty($StoreImg->store_industrylicense_img))
                    return $this->serviceError('请上传开户许可证照片');

                // 开户许可证 企业商户需要
                $queryParam['reg_acct_pic'] = $this->uploadImg($StoreImg->store_industrylicense_img,'F08',$configInfo);
                // 开户核准号
                $queryParam['open_licence_no'] = '企业进件-必填开户核准号';
            }

            /**
             * 上级汇付ID
             */
            $queryParam['head_huifu_id']= $superiorStoreInfo->mch_id;
            Log::info('汇付渠道-商户开户请求数据',$queryParam);
        }catch (\Exception $e){
            Log::info('汇付支付渠道-请求参数组装异常'.$e->getMessage());
            return $this->serviceError($e->getMessage());
        }

        /**
         * 请求发起
         */
        try {
            $queryResult = HuifuBaseController::getInstance()->basicdataEnt($configInfo,$queryParam);

            if (empty($queryResult))
                return $this->serviceError('申请失败');

            $arr = json_decode($queryResult,1);
            if ($arr['data']['resp_code'] != '200')
                return $this->serviceError($arr['data']['resp_desc']);

            /**
             * 进件成功后保存汇付侧商户号
             * huifu_id、apply_no、token_no
             */
            $dataArr = [
                'config_id'=>1234,
                'store_id'=>$Store->store_id,
                'mch_id'=>$arr['data']['mch_id'],
                'token_no'=>$arr['data']['token_no'],
            ];

            /**
             * 有申请单号代表该进件需要审核
             */
            if (isset($arr['data']['apply_no']))
            {
                $dataArr['apply_no'] = $arr['data']['apply_no'];
            }
            HuifuStoreRepo::getInstance()->saveData($dataArr);

            /**
             * 写入支付配置
             */
            $data['store_id'] = $Store->store_id;
            $data['rate'] = $rate;
            $data['status'] = 1;
            $data['status_desc'] = '开通成功';
            $data['company'] = 'huifu';
            return $this->send_ways_data($data);
        }catch (\Exception $e){
            Log::info('汇付支付渠道-接口请求异常'.$e->getMessage());
            return $this->serviceError('申请失败');
        }
    }

    /**
     * 个体进件
     * @param $Store
     * @param $StoreBank
     * @param $StoreImg
     * @param $rate
     * @param int $head_office_flag  0不挂靠 1挂靠
     * @return mixed
     * @throws \Exception
     */
    public function basicdataIndvinfoService($Store,$StoreBank,$StoreImg,$rate,$head_office_flag = 0)
    {

        /**
         * 获取渠道配置信息
         */
        $configInfo = HuifuConfigs::query()->where('config_id',1234)->first();
        if (empty($configInfo))
            return $this->serviceError('汇付渠道配置不存在');

        $configInfo = $configInfo->toArray();

        $data['req_seq_id'] = date('YmdHis').mt_rand();
        $data['req_date'] = date('Ymd',time());
        $data['upper_huifu_id'] = $configInfo['sys_id'];
        $data['reg_name'] = $Store->store_name;
        $data['short_name'] = $Store->store_short_name;

        /**
         * 根据分类ID获取分类名称
         */
        $categoryInfo = VbillStoreCategory::query()->where('id',$Store->category_id)->first();
        Log::info('汇付渠道商户开户-根据分类ID获取信息'.json_encode($categoryInfo,JSON_UNESCAPED_UNICODE));
        if (empty($categoryInfo))
            return $this->serviceError('门店分类不存在');
        $mccCode = HuifuMccCode::query()->where('mcc_name',$categoryInfo->category_name)->first();
        if (empty($mccCode))
            return $this->serviceError('汇付行业编码未找到');
        $data['mcc'] = $mccCode->mcc_code;
        $data['scene_type'] = 'ALL';

        /**
         * 获取省市区编码
         * PS：三方支付的省市区编码均一样，可区随行付、乐刷的数据库都可以
         */
        $provinceInfo = LeshuaUnionAreaCodeService::getInstance()->getByWhereService(['area_name'=>[$Store->province_name,$Store->city_name,$Store->area_name]]);
        if($provinceInfo->isEmpty())
            return $this->serviceError('城市编码缺省配置');
        $provincedata = $provinceInfo->toArray();
        $data['prov_id'] = $provincedata[0]['area_code'];
        $data['area_id'] = $provincedata[1]['area_code'];
        $data['district_id'] = $provincedata[2]['area_code'];
        $data['detail_addr'] = $Store->store_address;

        $data['legal_cert_no'] = $Store->head_sfz_no;
        $data['legal_cert_begin_date'] = date('Ymd',strtotime($Store->head_sfz_stime));
        $data['legal_cert_validity_type'] = $Store->head_sfz_time == '2999-12-31' ? "1":'0';
        $data['legal_cert_end_date'] = date('Ymd',strtotime($Store->head_sfz_time));
        $data['legal_addr'] = $Store->store_address;
        $data['legal_cert_back_pic'] = $this->uploadImg($StoreImg->head_sfz_img_b,'F03',$configInfo);
        $data['legal_cert_front_pic'] = $this->uploadImg($StoreImg->head_sfz_img_a,'F02',$configInfo);

        $data['contact_mobile_no'] = $Store->people_phone;
        $data['contact_email'] = $Store->store_email;
        $data['card_info'] = '2';

        // 对私银行卡正面
        $data['settle_card_front_pic'] = $this->uploadImg($StoreImg->bank_img_a,'F13',$configInfo);

        $data['sms_send_flag']='';

        //                // 店铺门头照片
//                'store_header_pic'=>$this->uploadImg($StoreImg->store_logo_img,'F22',$configInfo),
//                // 店铺内景/工作区域照
//                'store_indoor_pic'=>$this->uploadImg($StoreImg->store_img_c,'F24',$configInfo),
//                // 店铺收银台/公司前台照
//                'store_cashier_desk_pic'=>$this->uploadImg($StoreImg->store_img_a,'F105',$configInfo),
        $data['store_header_pic']='1';
        $data['store_indoor_pic']='2';
        $data['store_cashier_desk_pic']='3';


        /**
         * 请求发起
         */

        try {
            $queryResult = HuifuBaseController::getInstance()->basicdataEnt($configInfo,$data);

            if (empty($queryResult))
                return $this->serviceError('申请失败！');
            $arr = json_decode($queryResult,1);
            if ($arr['data']['resp_code'] != '00000000')
                return $this->serviceError($arr['data']['resp_desc']);
            /**
             * 进件成功后保存汇付侧商户号
             * huifu_id、apply_no、token_no
             */
            $dataArr = [
                'config_id'=>1234,
                'store_id'=>$Store->store_id,
                'mch_id'=>$arr['data']['huifu_id'],
                'token_no'=>$arr['data']['token_no'],
            ];
            HuifuStoreRepo::getInstance()->saveData($dataArr);
            return $this->serviceSuccess('SUCCESS');
        }catch (\Exception $e){
            Log::info('汇付支付渠道-个人进件接口请求异常'.$e->getMessage());
            return $this->serviceError('申请失败！');
        }
    }

    /**
     * 上传图片
     * @param string $file
     * @param string $file_type
     * @param $info
     * @return mixed
     * @throws \Exception
     */
    protected function uploadImg(string $file,string $file_type,$info)
    {
        $data = [
            'req_seq_id'=>date('YmdHis'),
            'req_date'=>date('Ymd'),
            'file_type'=>$file_type,
        ];

        $result = HuifuBaseController::getInstance()->picture($info,$file,$data);
        $array = json_decode($result,true);
        if ($array['data']['resp_code'] != '00000000')
            throw new \Exception($array['data']['resp_desc']);

        /**
         * 记录日志
         */
        $img_desc = '';
        if ($file_type == 'F07')
            $img_desc = '上传营业执照照片';
        if ($file_type == 'F03')
            $img_desc = '上传法人身份证国徽面';
        if ($file_type == 'F02')
            $img_desc = '上传法人身份证人像面';
        if ($file_type == 'F08')
            $img_desc = '上传开户许可证';
        if ($file_type == 'F13')
            $img_desc = '上传对私银行卡正面';
        if ($file_type == 'F56')
            $img_desc = '上传持卡人身份证国徽面';
        if ($file_type == 'F55')
            $img_desc = '上传持卡人身份证人像面';
        if ($file_type == 'F15')
            $img_desc = '上传授权委托书';
        if ($file_type == 'F22')
            $img_desc = '上传店铺门头照';
        if ($file_type == 'F24')
            $img_desc = '上传店铺内景/工作区域照';
        if ($file_type == 'F105')
            $img_desc = '上传店铺收银台/公司前台照';

        Log::info('汇付支付渠道-'.$img_desc.'-返回: '.$array['data']['file_id']);
        return $array['data']['file_id'];
    }

    /**
     * 获取进件状态
     * @param GetChannelDetailsParams $params
     * @return array|false|string
     * @throws BizException
     */
    public function getStatusQuery(GetChannelDetailsParams $params)
    {
        $info = HuifuStoreRepo::getInstance()->getByStoreId($params->getStoreId());
        if (empty($info))
            return $this->responError('商户信息不存在');

        /**
         * 获取渠道配置信息
         */
        $configInfo = HuifuConfigs::query()->where('config_id',1234)->first();
        if (empty($configInfo))
            return $this->responError('汇付渠道配置不存在');
        $configInfo = $configInfo->toArray();
        /**
         * 组装请求参数
         */
        $data = [
            'req_seq_id'=>date('YmdHis').mt_rand(),
            'req_date'=>date('Ymd'),
            'huifu_id'=>$info->huifu_id,
        ];

        /**
         * 有申请单号
         * PS:代表资料需要审核
         */
        if (!empty($info->apply_no))
        {
            $data['apply_no'] = $info->apply_no;
            $res = HuifuBaseController::getInstance()->statusQuery($configInfo,$data);
            $result = json_decode($res,1);
            if ($result['data']['resp_code'] != '00000000')
                return $this->responError($result['data']['resp_desc']);

            /**
             * 更新认证状态
             * Y：审核通过，P：审核中，N：审核拒绝，F：系统处理失败；示例值：Y
             */
            $status = $result['data']['apply_status'];
            $message = $result['data']['apply_reason'];
        }else{
            /**
             * 代表资料不需要审核
             */
            $status = 'Y';
            $message = 'success';
        }

        /**
         * 通过商户状态码更新
         */
        if (in_array($status,['P','F']))
        {
            $message = $status == 'P'?'汇付渠道审核中,请稍后':'汇付渠道系统处理失败,请联系开发人员';
            return $this->responSuccess($message);
        }

        $where = [ 'store_id' => $params->getStoreId(),"company"=>"huifu" ];
        if ($status == 'Y')
        {
            $data = [ 'status' => 1, 'status_desc' => '开通成功' ,'updated_at'=>date('Y-m-d H:i:s') ];
        }
        if ($status == 'N'){
            $data = [ 'status' => 3, 'status_desc' => '开通失败'.$message,'updated_at'=>date('Y-m-d H:i:s') ];
        }

        $res = StorePayRepo::getInstance()->savaData($where,$data);
        if (!$res)
            return $this->responError('系统异常');

        /**
         * 商户状态码
         * 0 待审核
         * 1 审核通过
         * 2 审核和不通过
         * 4 资料待完善
         * -1 未知
         */
        return ['status'=>$data['status'],'message'=>$message];
    }

    public function getQrCode(GetChannelDetailsParams $params)
    {
        /**
         * 获取渠道配置信息
         */
        $configInfo = HuifuConfigs::query()->where('config_id',1234)->first();
        if (empty($configInfo))
            return $this->responError('汇付渠道配置不存在');
        $configInfo = $configInfo->toArray();

        $info = HuifuStoreRepo::getInstance()->getByStoreId($params->getStoreId());
        if (empty($info))
            return $this->responError('商户信息不存在');

        /**
         * 组装请求参数
         */
        $data = [
            'req_seq_id'=>date('YmdHis').mt_rand(),
            'req_date'=>date('Ymd'),
            'apply_no'=>$info->apply_no,
            'huifu_id'=>$info->mch_id,
        ];
        $res = HuifuBaseController::getInstance()->statusQuery($configInfo,$data);
        $result = json_decode($res,1);
        if ($result['data']['resp_code'] != '000000')
            return $this->responError($result['data']['resp_desc']);

        /**
         * 需要保存微信、支付、银联等二级商户号
         * PS：咨询汇付侧实名认证二维码有效性,如长期有效可以保存或不保存
         * 返回客户端二维码、审核不通过的原因
         */
    }

    /**
     * 写入支付配置
     * @param $data
     * @return array|string[]
     */
    protected function send_ways_data($data)
    {
        try {
            //开启事务
            $all_pay_ways = DB::table('store_ways_desc')->where('company', $data['company'])->get();
            foreach ($all_pay_ways as $k => $v) {
                $gets = StorePayWay::where('store_id', $data['store_id'])
                    ->where('ways_source', $v->ways_source)
                    ->get();
                $count = count($gets);
                $ways = StorePayWay::where('store_id', $data['store_id'])
                    ->where('ways_type', $v->ways_type)
                    ->first();
                try {
                    DB::beginTransaction();
                    $data = [
                        'store_id' => $data['store_id'],
                        'ways_type' => $v->ways_type,
                        'company' => $v->company,
                        'ways_source' => $v->ways_source,
                        'ways_desc' => $v->ways_desc,
                        'sort' => ($count + 1),
                        'rate' => $data['rate'],
                        'settlement_type' => $v->settlement_type,
                        'status' => $data['status'],
                        'status_desc' => $data['status_desc'],
                    ];
                    if ($ways) {
                        $ways->update(
                            [
                                'status' => $data['status'],
                                'status_desc' => $data['status_desc']
                            ]);
                        $ways->save();
                    } else {
                        StorePayWay::create($data);
                    }
                    DB::commit();
                } catch (\Exception $e) {
                    Log::info('入库通道55');
                    Log::info($e->getMessage().' | '.$e->getFile().' | '.$e->getLine());
                    DB::rollBack();
                    return [
                        'status' => 2,
                        'message' => '通道入库更新失败1',
                    ];
                }
            }


            return [
                'status' => 1,
                'message' => $data['status_desc'],
            ];

        } catch (\Exception $e) {
            return [
                'status' => '-1',
                'message' => '通道入库更新失败2',
            ];
        }
    }
}